package modeleditor.editor;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.eclipse.swt.widgets.Composite;

import java.awt.Color;
import java.awt.Frame;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.*;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.activity.Activity;
import prefuse.controls.*;
import prefuse.data.*;
import prefuse.render.*;
import prefuse.util.*;
import prefuse.util.display.DisplayLib;
import prefuse.visual.*;
import topochart.Root;
import topochart.TheTopochart;

public class prefuse {

	Visualization vis;
	Graph graph=new Graph();
	Display display=new Display();

	public prefuse(JPanel frame, TheTopochart topo)
	{
		Table t1=new Table();
		ArrayList<String> name = new ArrayList<String>();
		ArrayList<String> value = new ArrayList<String>();
		name.add(topo.getRoot().getRname());
		for(int i=0; i<topo.getRoot().getNodes().size(); i++){
			name.add(topo.getRoot().getNodes().get(i).getNname());
		}
		value.add(topo.getRoot().getValue());
		for(int i=0; i<topo.getRoot().getNodes().size(); i++){
			value.add(topo.getRoot().getNodes().get(i).getValue());
		}
		for(int i=0; i<name.size(); i++){
			t1.addColumn(name.get(i), String.class);
		}
		t1.addRow();
		for(int i=0; i<value.size(); i++){
			t1.setString(0, i, value.get(i));
		}
		/*
		t1.addColumn("Person", String.class);
		t1.addColumn("Titel", String.class);
		t1.addColumn("FirstName", String.class);
		t1.addColumn("SecondName", String.class);
		t1.addColumn("Gender", String.class);
		t1.addColumn("Birthday", String.class);
		t1.addColumn("Department", String.class);
		t1.addColumn("Function", String.class);
		t1.addColumn("Boss", String.class);
		t1.addColumn("Telephon", String.class);
		t1.addColumn("Handy", String.class);
		t1.addColumn("Tele_Home", String.class);
		*/
		/*
		t1.setString(0, 0, "Tom Hans");
		t1.setString(0, 1, "Dr.");
		t1.setString(0, 2, "Tom");
		t1.setString(0, 3, "Hans");
		t1.setString(0, 4, "male");
		t1.setString(0, 5, "1978-05-09");
		t1.setString(0, 6, "Marketing");
		t1.setString(0, 8, "Anny Tiger");
		t1.setString(0, 9, "0194537653");
		t1.setString(0, 10, "0191345667828");
		*/
		//***Start: Graph data init
		
		graph=new Graph();		
		graph.addColumn("data", String.class);
		graph.addColumn("column_name", String.class);
		graph.addColumn("id", int.class);
		graph.addColumn("toShow", String.class);
		for(int i=0;i<t1.getColumnCount();i++)
		{
			Node n=graph.addNode();
			n.setString("data", String.valueOf(t1.get(0, i)));
			n.setString("column_name", t1.getColumnName(i));
			n.setInt("id", i);
			n.setString("toShow", n.get(1)+":\n"+n.get(0));
		}
		for(int i=1;i<t1.getColumnCount();i++)
		{
			graph.addEdge(0, i);
			//**Start: Edges with different length
		}
		
		Table edg1=graph.getEdgeTable();
		System.out.println(graph.getEdgeCount()+"  "+edg1.getRowCount());
		edg1.addColumn("length", float.class);
		
		for(int i=0;i<edg1.getRowCount();i++)
		{
			edg1.setFloat(i, "length", 50+i*20);
			System.out.println("edg1.get(i, 0): "+edg1.get(i, 0)+" edg1.get(i, 1): "+edg1.get(i, 1)+
			" edg1.get(i, 6): "+edg1.get(i, 6)+"  "+edg1.getColumnName(6)+"  i: "+i+"  "+edg1.getRowCount());
		}
		
		//****End: Edges with different length
		//******End: Graph data init
		//**Start: Label
		LabelRenderer lr=new LabelRenderer("toShow");
		lr.setRoundedCorner(8, 8);
		//****End: Label
		//**Start: Edge shap
		EdgeRenderer er=new EdgeRenderer(Constants.EDGE_TYPE_LINE, Constants.EDGE_ARROW_NONE);
		//****End: Edge shap
		vis = new Visualization();
		vis.add("graph", graph);
		vis.setRendererFactory(new DefaultRendererFactory(lr, er));
		//**Start: data color
		DataColorAction fill = new DataColorAction("graph.nodes", "id",
		Constants.NOMINAL, VisualItem.FILLCOLOR);
		ColorAction textColor = new ColorAction("graph.nodes",
		VisualItem.TEXTCOLOR, ColorLib.gray(0));
		ColorAction edgesColor = new ColorAction("graph.edges",
		VisualItem.STROKECOLOR, ColorLib.gray(200));
		ActionList color = new ActionList();
		color.add(fill);
		color.add(textColor);
		color.add(edgesColor);
		//****End: data color
		//ForceDirectedLayout: definiert edges length
		ActionList layout = new ActionList(Activity.INFINITY);
		MyForceDirectedLayout fdl=new MyForceDirectedLayout("graph", 170);
		layout.add(fdl);
		layout.add(new RepaintAction());
		vis.putAction("color", color);
		vis.putAction("layout", layout);
		display_init();
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//frame.setTitle("prefuse");
		frame.setSize(800, 800);
		frame.add(display);
		//frame.setResizable(false);
		frame.setVisible(true);
	}

	public void display_init()
	{
		display.setVisualization(this.vis);
		display.setHighQuality(true);
		display.setSize(800, 800);
		display.setBackground(Color.WHITE);
		display.addControlListener(new FocusControl(1));
		display.addControlListener(new DragControl(false, true));
		display.addControlListener(new PanControl());
		display.addControlListener(new WheelZoomControl());
		final ZoomToFitControl zfc=new ZoomToFitControl(this.vis.ALL_ITEMS, 40, 2000, Control.RIGHT_MOUSE_BUTTON);
		display.addControlListener(zfc);
		display.addControlListener(new NeighborHighlightControl());
	}

	public void ZoomToFitByStart()
	{
		//ZoomToFit
		boolean b=true;
		if ( !display.isTranformInProgress() && b)
		{
			b=false;
			String m_group = this.vis.ALL_ITEMS;
			long m_duration = 2000;
			Rectangle2D bounds = this.vis.getBounds(m_group);
			GraphicsLib.expand(bounds, 190 + (int)(1/display.getScale()));
			DisplayLib.fitViewToBounds(display, bounds, m_duration);
		}
	}
	/*
	public static prefuse drawprefuse() {
		prefuse frame = new prefuse("Prefuse");
		frame.vis.run("color");
		frame.vis.run("layout");
		frame.ZoomToFitByStart();
		return frame;
	}
	*/
}

class MyForceDirectedLayout extends ForceDirectedLayout
{
	float f;
		public MyForceDirectedLayout(String graph) {
		super(graph);
	}
	
	public MyForceDirectedLayout(String graph, float f) {
		super(graph);
		this.f=f;
	}
	
	@Override
	protected float getSpringLength(EdgeItem e)
	{
		float f=(float) 0;
		f=e.canGetFloat("length")?e.getFloat("length"):200;
		return (float) f;
	}
}